// JavaScript Document
(function(){
	var defaults={
		url:"",
		parm:{}
	};
	//子菜单集合,url地址数组
	var urList = [],urPath = [];
	$.drawMenu = function(options){
		var opts =  $.extend({},defaults,options);
		//主菜单
		$menu = $(".nav-menu");
		//子菜单
		$childMenu = $("#panel-left");
		//主面板
		$panelRight = $("#panel-right");

		$.ajax({
			url:opts.url,
			type:'post',
			DataType:'json',
			success:function(data){
				var obj=eval('(' +data+ ')');
				//调用绘制菜单
				$menu.html(drawNavMenu(obj));
				changeMenu();
				//窗口缩放，调用菜单响应
				$(window).on("resize",function(){
					changeMenu();
				});
				function stopPro(evt){
					//阻止冒泡
					var e=(evt)?evt:window.event;
					if (window.event) { 
						e.cancelBubble=true;
						e.stopPropagation();
					} else { 
						e.stopPropagation(); 
					}	
				};
				function changeMenu(){
					if($(".tempLi").size()<=0){
						$("#firstMenu").append("<li class='tempLi'></li>");
					}
					$("#showMore ul").html("");
					var x = $("#firstMenu").width()/75;
					x = parseInt(x);
					if(x<$("#firstMenu>li").size()){
						if($("#showMore").size()<=0){
							$menu.append("<div id='showMore' title='更多' style='position:absolute;right:0px; top:20px;'><button class='moreBtn'><i class='icon-angle-down'></i></button><ul class='moreList'></div>");
							var cxx = 0;
							$(".moreBtn").on("click",function(evt){
								if(cxx == 0){
									if($(this).next().is(":hidden")){
										$(this).next().show();
										cxx = 1;
									}
								}else{
									$(this).next().hide();
									cxx = 0;
								}
								stopPro(evt)
							})
						}
						$("#firstMenu>li").show();
						
						for(var i = x;i<$("#firstMenu>li").size()-1;i++){
							$(".moreList").append($("#firstMenu>li").eq(i).clone());
							$("#firstMenu>li").eq(i).hide();
						}
						//点击事件
						$(".moreList li").on("click",function(evt){
							$("#firstMenu>li").eq(x+$(this).index()).click();
							stopPro(evt); 
						})
					}else{
						$("#showMore").remove();
					}
				};
				//主菜单点击
				$menu.off().on("click","#firstMenu li",function(evt){
                    if($(this).attr('class')=='tempLi'){
                        return;
                    }
					$menu.find("ul li").removeClass("menuActive");
					$(this).addClass("menuActive");
					var tempGetChild =  urList[$(this).index()];
					if(tempGetChild){
						$childMenu.html(drawChildMenu(tempGetChild));
						//模拟点击初始化
						$("#childMenu li").eq(0).addClass("childActive");
						$panelRight.find("iframe").attr("src",urPath[0]);
						
						if($panelRight.css("left")=="0px"){
							
							//判断iframe是否加载完成
							var iframe = document.getElementById("iframe");
							if (iframe.attachEvent){
								iframe.attachEvent("onload", function(){
									$panelRight.animate({"left":"160px"},1000);
								});
							} else {
								iframe.onload = function(){
									$panelRight.animate({"left":"160px"},1000);
								};
							}
							
						}					
					}
					stopPro(evt);
				});
				//子菜单点击
				$("#panel").on("click","#childMenu li",function(){
					$childMenu.find("ul li").removeClass("childActive");
					$(".sildeMenu").slideUp();
					$(this).addClass("childActive");
					if($(this).find(".sildeMenu").is(":hidden")){
						sildeList($(this).find(".sildeMenu"));
					}else{
						$panelRight.find("iframe").attr("src",urPath[$(this).index()]);
					}
				});
				$childMenu.on("click",".sildeMenu li",function(evt){
					var x = $(this).parent().parent().index()+$(this).index();
					if($(this).parent().parent().prevAll().children(".sildeMenu").is(":hidden")){
						x+=	$(this).parent().parent().prevAll().children(".sildeMenu").find("li").size()-$(this).parent().parent().prevAll().children(".sildeMenu").size();
					}
					$panelRight.find("iframe").attr("src",urPath[x]);
					stopPro(evt); 
				});
				//模拟点击初始化-临时解决方案
				//$("#firstMenu li").eq(0).click();
			},
			error: function(data){
			}
		})
		
	};
	//展开子菜单
	function sildeList($dom){
		$dom.is(":visible")?$dom.slideUp() : $dom.slideDown();
	}
		
	//拼接主菜单及保存子菜单
	drawNavMenu = function(menuData){
		var str="<ul id='firstMenu'>";
		for(var i =0;i < menuData.length;i++){
			urList[i] = {};
			//判断是否子菜单，然后缓存进全局变量
			if(menuData[i].child){
			//临时变量
				var childList = menuData[i].child,tempUrlList = [],tempNameList = [],tempChildList = [],x = 0;
				for(var j = 0;j<childList.length;j++){
					if(childList[j].child){
						urList[i][j] = {"name":childList[j].name,"child":childList[j].child}
					}else{
						urList[i][j] = {"name":childList[j].name,"url":childList[j].url}
					}
				}
			}
			str+="<li>"+menuData[i].name+"</li>";
		}
		str+="</ul>";
		return str;
	};
	//拼接子菜单
	drawChildMenu = function(menuData){
		urPath = [];
		var str = "<ul id='childMenu'>";
		for(var i = 0;i<100;i++){
			if(menuData[i]){
				//缓存url地址
				if(menuData[i].child){
					str+="<li>"+menuData[i].name;
					str +="<br/><ul class='sildeMenu'>"
					for(var j = 0;j<menuData[i].child.length;j++){
						str+="<li>"+menuData[i].child[j].name+"</li>";
						urPath.push(menuData[i].child[j].url);
					}
					str+="</ul></li>"
				}else{
					str+="<li>"+menuData[i].name+"</li>";
					urPath.push(menuData[i].url);	
				}
			}else{
				break;	
			}
			
		}
		str+="</ul>";
		return str;
	}
})(jQuery);
